package com.shop.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.constant.MessageConstant;
import com.shop.entity.QueryPageBean;
import com.shop.entity.Result;
import com.shop.pojo.Address;
import com.shop.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/address")
@Api(tags = "地址管理")
public class AddressController {
    @Reference
    private AddressService addressService;

//    @PreAuthorize("hasAuthority('ADD_ADDRESS')")
    @PostMapping("/add")
    @ApiOperation("添加地址接口")
    public Result add(@RequestBody Address address){
        if (addressService.findAddressName(address.getName())!=null){
            return new Result(false,MessageConstant.ADD_ADDRESS_FAIL);
        }else{
            addressService.add(address);
            return new Result(true,MessageConstant.ADD_ADDRESS_SUCCESS);
        }
    }

    @PreAuthorize("hasAuthority('QUERY_ADDRESS')")
    @PostMapping("/pageQuery")
    @ApiOperation("地址分页接口")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        Page<Address> page=addressService.pageQuery(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,page);
    }

//    @PreAuthorize("hasAuthority('DELETE_ADDRESS')")
    @DeleteMapping("/delete")
    @ApiOperation("删除地址接口")
    @ApiImplicitParams({@ApiImplicitParam(dataType = "int",name = "id",value = "地址id",required = true,example = "0")})
    public Result delete(Integer id){
        try {
            addressService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
    }

//    @PreAuthorize("hasAuthority('EDIT_ADDRESS')")
    @PutMapping("/edit")
    @ApiOperation("修改地址接口")
    public Result edit(@RequestBody Address address){
//        try {
//            addressService.edit(address);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
//        }
//        return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS);
        Address a = addressService.findById(address.getId());
        if (addressService.findAddressName(address.getName())!=null && !a.getName().equals(address.getName())){
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }else{
            addressService.edit(address);
            return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS);
        }
    }

    @GetMapping("/findById")
    @ApiOperation("根据地址id查找地址接口")
    public Result findById(@RequestParam(required = true) Integer id){
        try {
            Address address=addressService.findById(id);
            return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
        }
    }

//    @GetMapping("/findAll")
//    public Result findAll(){
//        try {
//            List<Address> list=addressService.findAll();
//            return new Result(true,MessageConstant.QUERY_ADDRESS_SUCCESS,list);
//        }catch (Exception e){
//            e.printStackTrace();
//            return new Result(false,MessageConstant.QUERY_ADDRESS_FAIL);
//        }
//    }

}
